define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	var fn = require("$UI/ls_common/fn");
	var Model = function() {
		this.callParent();
		this.SaleApp_ret = {};
		this.SDID = 0;
		this.total_upload_times = 0;
		this.finish_upload_times = 0;
	};
	Model.prototype.modelLoad = function(event) {
		console.log("modelLoad()");
	};
	Model.prototype.modelParamsReceive = function(event) {
		console.log("modelParamsRecive");
		console.log(event.params);
		this.SDID = event.params.SDID;
		$.extend(this.SaleApp_ret, event.params.SaleApp_ret);
		console.log(this.SaleApp_ret);
		this.TaskInit();
	};
	Model.prototype.ReCount = function(ID, PackingRatio) {
		console.log("ReCount()");
		var count = 0;
		// var self = this;
		console.log(ID);
		this.comp("BarCode_Data").count(function(ev) {
			console.log(ev.source.getValue("ID", ev.row));
			if (ID == ev.source.getValue("ID", ev.row)) {
				var v = ev.source.getValue('Type', ev.row);
				if (parseInt(v) === 0)
					count += parseInt(PackingRatio.substr(2));
				if (parseInt(v) === 1)
					count += 1;
			}
		});
		return count;
	};
	Model.prototype.TaskInit = function() {
		console.log("TaskInit");
		this.comp("ID_LIST_Data").clear();
		for (var i = 0; i < Object.keys(this.SaleApp_ret).length; i++) {
			console.log(this.SaleApp_ret[i].SDID);
			if (this.SaleApp_ret[i].SDID == this.SDID) {
				this.comp("ID_LIST_Data").loadData([ this.SaleApp_ret[i] ], true);
				fn.cat_x5_data(this.comp("ID_LIST_Data"));
			}
		}
		var data = {};
		data.username = window.app.username;
		data.password = window.app.password;
		data.ID = this.SDID;

		var self = this;
		this.comp("BarCode_Data").clear();
		var api_url = window.app.base_url + window.app.SaleBarcodeApi;
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.log(res);
				var count = Object.keys(res).length;
				console.time("BarCode_add");
				for (var i = 0; i < count; i++) {
					// console.log(res[i]);

					self.comp("BarCode_Data").add({
						'code' : res[i].Pouchcode,
						'Type' : 1,
						'ID' : res[i].SDInfoID
					});
				}
				console.timeEnd("BarCode_add");
				fn.cat_x5_data(self.comp("BarCode_Data"));
				return;
			},
			error : function(err) {
				console.log(err);
				return;
			}
		});
	};
	Model.prototype.SendButtonClick = function(event) {
		var self = this;
		self.total_upload_times = self.comp("ID_LIST_Data").count();
		self.finish_upload_times = 0;
		var data = {};
		data.SDID = self.SDID;
		data.SDCode = "aaaa";
		data.BarCode = [];
		data.PrdList = [];
		self.comp("BarCode_Data").each(function(param) {
			data.BarCode.push({
				ID : param.row.val('ID') + "",
				Type : param.row.val('Type'),
				code : param.row.val('code')
			});
		});
		self.comp("ID_LIST_Data").each(function(param_P) {
			data.PrdList.push({
				ID : param_P.row.val("ID"),
				Batch : param_P.row.val("Batch"),
				IsBool : param_P.row.val("IsBool") === undefined ? 0 : 1,
			});
		});
		console.log(data);
		var api_url = window.app.base_url + window.app.SaleAddApp;
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.log(res[0]);
				if (res[0].errno != "0") {
					justep.Util.hint("数据上传出错，请联络管理员。");
					return;
				}
				self.finish_upload_times += 1;
				if (self.finish_upload_times == self.finish_upload_times) {
					self.getParent().comp("FactoryContents").to("TaskListContent");
				}
				return;
			},
			error : function(err) {
				console.log(err);
				return;
			}
		});
	};
	Model.prototype.QRScan_ButtonClick = function(event) {
		console.log(event);
		var SubID = event.bindingContext.$object.val("ID");
		var IsBool = event.bindingContext.$object.val("IsBool");
		var PackingRatio = event.bindingContext.$object.val("PackingRatio");
		if (IsBool === undefined)
			IsBool = false;
		var prdcount = IsBool === false ? event.bindingContext.$object.val("PrdCount") : event.bindingContext.$object.val("BatchSum") - event.bindingContext.$object.val("PrdCount");
		this.comp("ScanCodeWindowContainer").load("Factory/Warehouse/ScanCode.w", {
			SubID : SubID,
			IsBool : IsBool,
			PrdCount : prdcount,
			PackingRatio : PackingRatio
		});
		this.comp("contents1").to("ScanCodeContent");
	};

	Model.prototype.BackBtnClick = function(event) {
		this.getParent().comp("FactoryContents").to("TaskListContent");
	};
	return Model;
});